package servlet.redar;

import com.alibaba.fastjson.JSONObject;
import utils.JdbcUtil;
import utils.ResultData;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *      读者信息修改
 */
@WebServlet(urlPatterns = "/redarUpdate")
public class RedarUpdate extends HttpServlet {
    JdbcUtil jdbcUtil=new JdbcUtil();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id=Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        String user = req.getParameter("user");
        Connection connection = jdbcUtil.getConnection();
        String sql="update notice set name=?,content=?,user=? where id=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,content);
            preparedStatement.setString(3,user);
            preparedStatement.setInt(4,id);

            int i = preparedStatement.executeUpdate();
            ResultData resultData=null;
            if (i>0){
                resultData= ResultData.success();
            }else{
                resultData= ResultData.fail();
            }
            Object o = JSONObject.toJSON(resultData);
            resp.getWriter().print(o.toString());

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

}

